package com.ytzl.itrip.search.dao;

import com.ytzl.itrip.utils.common.Constants;
import com.ytzl.itrip.utils.common.EmptyUtils;
import com.ytzl.itrip.utils.common.Page;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2018/7/23.
 */
public class BaseQuery<T> {

    private HttpSolrClient solrClient;

    public BaseQuery(String baseUrl){
        solrClient = new HttpSolrClient(baseUrl);
        // 设置解析器
        solrClient.setParser(new XMLResponseParser());
        // 设置延迟时间
        solrClient.setConnectionTimeout(1000);
    }


    public Page<T> queryPage(SolrQuery solrQuery,Integer pageNo,Integer pageSize,
                             Class clazz)throws Exception{
        Integer curPage = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        Integer pSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        solrQuery.setStart((curPage-1)*pSize);
        solrQuery.setRows(pSize);
        // 获取查询返回结果
        QueryResponse queryResponse = solrClient.query(solrQuery);
        // 获取总条数
        Integer total = Long.valueOf(queryResponse.getResults().getNumFound()).intValue();
        Page<T> page = new Page<>(curPage,pSize,total);
        // 获取数据
        SolrDocumentList documents = queryResponse.getResults();
        List<T> list = new ArrayList();
        for (SolrDocument document:documents){
            list.add((T) toBean(document,clazz));
        }
        page.setRows(list);
        return page;
    }


    public List<T> queryList(SolrQuery solrQuery,Integer pageSize,Class clazz)throws Exception{
        if (EmptyUtils.isNotEmpty(pageSize)){
            solrQuery.setStart(0);
            solrQuery.setRows(pageSize);
        }
        QueryResponse queryResponse = solrClient.query(solrQuery);
        SolrDocumentList documents = queryResponse.getResults();
        List<T> list = new ArrayList();
        for (SolrDocument document:documents){
            list.add((T) toBean(document,clazz));
        }
        // 返回查询结果
        return (List<T>) list;
    }


    public static Object toBean(SolrDocument record, Class clazz){
        Object obj = null;
        try {
            obj = clazz.newInstance();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        }
        Field[] fields = clazz.getDeclaredFields();
        for(Field field:fields){
            Object value = record.get(field.getName());
            try {
                BeanUtils.setProperty(obj, field.getName(), value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

}
